package tests;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import ui.INickPanel;
import ui.IReadingPanel;
import ui.NickPanel;
import ui.ReadingPanel;
import ui.WritingPanel;
import client.Controller;
import client.IController;
import client.ISingleConversation;
import client.MultipleController;
import client.SingleController;
import client.SingleConversation;
import client.StreamReader;

public class ChatTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		boolean flag = true;
		
		while (flag==true){
		
			String host;
			String nickname = "";
			
			
			host = JOptionPane.showInputDialog("Inserisci host o ip del server", "127.0.0.1");
			
			
			JFrame frame = new JFrame("Chat by Java E Le Storie Tese - " + host);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
			try {
				
				
				Socket socket = new Socket();
				
				SocketAddress sockaddr = new InetSocketAddress(host, 50999);
				socket.connect(sockaddr, 2000);
				
				do {
					nickname = JOptionPane.showInputDialog("Inserisci il tuo NickName : ");
				} while (nickname.length() < 2 || nickname.isEmpty());
				
				
				frame.setLayout(new BorderLayout());
				
				IReadingPanel rP=new ReadingPanel();
				
				WritingPanel writingPanel=new WritingPanel(socket, nickname);
				ISingleConversation singleConversation = new SingleConversation(nickname,writingPanel.getBw(),writingPanel);
				
				
				SingleController singleController=new SingleController(singleConversation);
				MultipleController multipleController=new MultipleController();
				
				INickPanel nP=new NickPanel(singleConversation);
				
				IController controll=new Controller(nickname,rP,singleController,multipleController,nP);
				
				StreamReader readingPanel = new StreamReader(socket,controll);
				
				JPanel chatpanel = new JPanel();
				chatpanel.setLayout(new BorderLayout());
				
				chatpanel.add((Component) rP,BorderLayout.NORTH);
				chatpanel.add(writingPanel,BorderLayout.SOUTH);
				
				frame.add(chatpanel,BorderLayout.CENTER);
				frame.add((Component) controll.getNickPanel(),BorderLayout.EAST);
				
				Thread thread = new Thread(readingPanel);
				thread.start();
				
				flag=false;
				
				
			} catch (IOException e) {
				JOptionPane.showMessageDialog(frame, "Server " + host + 
						" non raggiungibile, riprova ad inserire l'indirizzo.", "Errore", JOptionPane.ERROR_MESSAGE);
	
			} catch (Exception e) {
				JOptionPane.showMessageDialog(frame, "Sessione Terminata.", "Arrivederci...", JOptionPane.INFORMATION_MESSAGE);
		
				System.exit(JOptionPane.CANCEL_OPTION);
				
			}
			
			if(flag==false){
			
				frame.setSize(500,300);
				frame.setVisible(true);
			}
			
		}
		
	}

}
